from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.shortcuts import reverse
from django.urls import resolve


# Create your views here.

def index(request):
    return HttpResponse("欢迎访问D_URL路由页面")


def greet(request, name):
    return HttpResponse(f"Hello, {name}!")


def show_age(request, age):
    return HttpResponse(f"You are {age} years old.")


def show_page(request, page_slug):
    return HttpResponse(f"Displaying page: {page_slug}")


def show_user_profile(request, user_uuid):
    return HttpResponse(f"User profile for UUID: {user_uuid}")


def getName(request, name, age):
    return HttpResponse(f"Hello, {name}! You are {age} years old.")


def getTime(request, year, month, day):
    return HttpResponse(f"Today is {year}-{month}-{day}")


def get_html_time(request):
    return render(request, 'Time.html')


def g_reverse(request, year, month, day):
    args = ['2024', '01', '01']
    result = resolve(reverse('D_URL:g_reverse', args=args))
    dic = {
        'kwargs': result.kwargs,
        'url_name': result.url_name,
        'namespace': result.namespace,
        'view_name': result.view_name,
        'app_name': result.app_name,
        'data': {
            'year': year,
            'month': month,
            'day': day
        }
    }
    print(dic)
    return HttpResponse(f"reverse:{dic}")


def g_resolve(request):
    args = ['2024', '01', '01']
    kwargs = {
        'year': '2024',
        'month': '02',
        'day': '01'
    }
    # 先使用reverse再使用resolve
    print(reverse('D_URL:g_reverse', kwargs=kwargs))
    print(reverse('D_URL:g_reverse', args=args))
    return HttpResponse(reverse('D_URL:g_reverse', kwargs=kwargs))


def a_b(request):
    # 重定向
    return redirect('D_URL:b')


def b(request):
    return HttpResponse("欢迎来到b页面")


def c_d(request):
    return HttpResponse("欢迎来到c页面")
